package com.raspbian.monitor.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.raspbian.monitor.entity.SensorData;
import com.raspbian.monitor.mapper.SensorDataMapper;
import com.raspbian.monitor.service.SensorDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorDataMapper, SensorData> implements SensorDataService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insertSensorData(SensorData sensorData) {
        sensorData.getHumidity();
        this.baseMapper.insert(sensorData);
        return R.ok("插入成功");
    }

    @Override
    public R selectLatestData() {
        QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time")
                .last("limit 3");
        try {
            List<SensorData> sensorData = this.list(queryWrapper);
            return R.ok(sensorData);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

    @Override
    public R selectAppData(Date startTime) {
        QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("create_time", startTime)
                .orderByDesc("create_time")
                .last("limit 6");
        try {
            List<SensorData> sensorData = this.list(queryWrapper);
            return R.ok(sensorData);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

    @Override
    public R selectTodaySensorData() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        QueryWrapper<SensorData> wrapper = new QueryWrapper<>();
        wrapper.ge("create_time", zero);
        try {
            List<SensorData> sensorData = this.list(wrapper);
            return R.ok(sensorData);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

    @Override
    public R selectSensorDataTable(Date startTime, Date endTime,
                                   Double minTemp, Double maxTemp,
                                   Double minHumidity, Double maxHumidity,
                                   Integer gasStatus) {
        if (StringUtils.isEmpty(startTime)) startTime = new Date(0L);
        if (StringUtils.isEmpty(endTime)) endTime = new Date();
        if (StringUtils.isEmpty(minTemp)) minTemp = 0d;
        if (StringUtils.isEmpty(maxTemp)) maxTemp = 100d;
        if (StringUtils.isEmpty(minHumidity)) minHumidity = 0d;
        if (StringUtils.isEmpty(maxHumidity)) maxHumidity = 100d;
        QueryWrapper<SensorData> wrapper = new QueryWrapper<>();
        wrapper.ge("create_time", startTime).le("create_time", endTime)
                .ge("temperature", minTemp).le("temperature", maxTemp)
                .ge("humidity", minHumidity).le("humidity", maxHumidity);
        if (!StringUtils.isEmpty(gasStatus)) wrapper.eq("gas", gasStatus);
        try {
            List<SensorData> sensorData = this.list(wrapper);
            return R.ok(sensorData);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

}
